package com.timmy.google_play;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.ViewTreeObserver;

import com.astuetz.PagerSlidingTabStrip;
import com.timmy.google_play.base.BaseFragment;
import com.timmy.google_play.base.LoadingPager;
import com.timmy.google_play.factory.FragmentFactory;
import com.timmy.google_play.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.drawlayout)
    DrawerLayout mDrawlayout;
    @BindView(R.id.tabStrip)
    PagerSlidingTabStrip mTabStrip;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    private ActionBarDrawerToggle mToggle;
    private String[] mTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initActionBar();
        initData();
        initListener();
    }

    private void initListener() {

        final MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener();

        mTabStrip.setOnPageChangeListener(myOnPageChangeListener);

        mViewpager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //手动选中第一页
                myOnPageChangeListener.onPageSelected(0);

                //移除
                mViewpager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

    }

    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            //页面选中的时候
            //触发加载数据
            BaseFragment fragment = FragmentFactory.map.get(position);

            LoadingPager loadingPager = fragment.getLoadingPager();

            loadingPager.triggerLoadData();

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private void initData() {

        mTitles = UIUtils.getStrings(R.array.main_titles);

        MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());

        mViewpager.setAdapter(adapter);

        //---------------
        mTabStrip.setViewPager(mViewpager);
    }

    class MyFragmentAdapter extends FragmentStatePagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment  = FragmentFactory.createFragment(position);

            return fragment;
        }

        @Override
        public int getCount() {
            if (mTitles != null) {
                return mTitles.length;
            }

            return 0;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    private void initActionBar() {
        //1.获取Actionbar对象
        ActionBar actionBar = getSupportActionBar();

        //2.设置标题
        actionBar.setTitle("GooglePlay");
//        actionBar.setSubtitle("副标题");

        //3.设置图标
        actionBar.setIcon(R.mipmap.ic_launcher);
        actionBar.setLogo(R.mipmap.ic_action_locate);

        //4.设置是否显示图标,默认是隐藏的
        actionBar.setDisplayShowHomeEnabled(false);

        //5.设置icon和logo显示的优先级,默认是icon优先
        actionBar.setDisplayUseLogoEnabled(true);

        //6.显示回退部分
        actionBar.setDisplayHomeAsUpEnabled(true);

        initActionBarDrawerToggle();
    }

    private void initActionBarDrawerToggle() {
        mToggle = new ActionBarDrawerToggle(this, mDrawlayout, R.string.open, R.string.close);

        //同步状态
        mToggle.syncState();

        //监听DrawLayout的拖动
        mDrawlayout.addDrawerListener(mToggle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                //点击Toggle 可以切换drawerlayout的打开和关闭
                mToggle.onOptionsItemSelected(item);
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
